package com.ruoyi.framework.web.domain;

/**
 * @author qck
 * @desc
 * @data 2020/12/10 15:23
 */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.CommonEnum;
import com.ruoyi.common.constant.SRCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author qck
 * @desc
 * @data 2020/8/19 10:41
 */
@Data
@ApiModel( description = "接口返回对象")
public class R<K> implements Serializable {

    /**
     * 返回码
     */
    @ApiModelProperty(value = "错误码,0成功",example = "0")
    private Object code;
    /**
     * 是否成功
     */
    @ApiModelProperty(value = "成功标志，true成功，false失败",example = "true")
    private boolean success;
    /**
     * 返回消息
     */
    @ApiModelProperty(value = "错误信息",example = "成功")
    private String msg;

    /**
     * 返回数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "返回数据")
    private K data;

    /**
     * 把构造方法私有
     */
    private R() {
    }

    public R setCode(Object code) {
        this.code = code;
        return this;
    }

    public R setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 把构造方法私有
     */
    private <T extends CommonEnum> R(boolean success, T status, K data) {
        this.success = success;
        this.code = status.getValue();
        this.msg = status.getDesc();
        setData(data);
    }

    /**
     * 把构造方法私有
     */
    private <T extends CommonEnum> R(boolean success, T status) {
        this.success = success;
        this.code = status.getValue();
        this.msg = status.getDesc();
    }

    private void setData(K data) {
        if (data instanceof Page) {
            this.data = (K) new PageResult((Page) data);
        } else
        {
            this.data = data;
        }
    }

    /**
     * 把构造方法私有
     */
    private <T extends CommonEnum> R(boolean success, T status, String msg) {
        this.success = success;
        this.code = status.getValue();
        this.msg = msg;
    }

    public static <T extends CommonEnum> R ok(T status, Object data) {
        return new R(true, status, data);
    }

    public static <T extends CommonEnum> R ok(T status) {
        return new R(true, status);
    }

    public static <T extends CommonEnum> R ok(Object data) {
        return new R(true, SRCode.SUCCESS, data);
    }

    public static <T extends CommonEnum> R ok() {
        return new R(true, SRCode.SUCCESS);
    }

    public static <T extends CommonEnum> R error(T status, Object data) {
        return new R(false, status, data);
    }

    public static <T extends CommonEnum> R error(Object data) {
        return new R(false, SRCode.ERR_500, data);
    }

    public static <T extends CommonEnum> R error(T status) {
        return new R(false, status);
    }

    public static <T extends CommonEnum> R error() {
        return new R(false, SRCode.ERR_500);
    }

    public <T extends CommonEnum> R code(T code) {
        this.code = code.getValue();
        this.msg = code.getDesc();
        return this;
    }

    @Deprecated
    public R code(Object code) {
        this.code = code;
        this.msg = "";
        return this;
    }

    @Deprecated
    public R message(String msg) {
        this.msg = msg;
        return this;
    }

    public R data(K data) {
        this.setData(data);
        return this;
    }
}

